package com.loong.shop.controller.backend;

import com.loong.common.domain.BaseQuery;
import com.loong.common.mvc.BaseBackendController;
import com.loong.common.param.BaseIdParam;
import com.loong.common.rest.Result;
import com.loong.common.validator.group.CommonInsert;
import com.loong.common.validator.group.CommonUpdate;
import com.loong.common.validator.group.CommonValidate;
import com.loong.shop.common.backend.param.GoodsBaseParam;
import com.loong.shop.common.backend.param.GoodsBaseQueryParam;
import com.loong.shop.common.backend.param.GoodsRecommendOrNotParam;
import com.loong.shop.handler.GoodsBaseHandler;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Description: 处理后台商品请求的Controller
 * @Author: 刘群
 * @Date: 2020/10/27 8:55
 */
@RestController
@RequestMapping("manage/shop/goodsBase")
public class GoodsBaseController extends BaseBackendController {

    @Resource
    private GoodsBaseHandler baseHandler;

    /**
     * 新增商品
     * @param param 商品封装的参数
     * @return 响应的结果
     */
    @PostMapping("save")
    public Result saveGoodsBase(@Validated({CommonInsert.class}) GoodsBaseParam param){
        param.setId(null);
        return handlerExecutor.execute(() -> baseHandler.saveGoodsBase(param));
    }

    /**
     * 更新商品
     * @param param 商品封装的参数
     * @return 响应的结果
     */
    @PostMapping("update")
    public Result updateGoodsBase(@Validated({CommonUpdate.class}) GoodsBaseParam param){
        return handlerExecutor.execute(() -> baseHandler.updateGoodsBase(param));
    }

    /**
     * 批量删除商品
     * @param idList 商品id集合
     * @return 响应的结果
     */
    @PostMapping("delete")
    public Result deleteGoodsBase(@RequestParam("idList") List<Long> idList){
        if (CollectionUtils.isEmpty(idList)) return null;
        return handlerExecutor.execute(() -> baseHandler.deleteGoodsBase(idList));
    }


    /**
     * 分页查询商品
     * @param query 查询参数
     * @return 响应的结果
     */
    @PostMapping("pageQuery")
    public Result pageQueryGoodsBase(BaseQuery query, GoodsBaseQueryParam param){
        return handlerExecutor.execute(() -> baseHandler.pageQueryGoodsBase(query,param));
    }

    /**
     * 推荐或取消推荐商品
     * @param param 商品推荐封装的参数
     * @return 响应的结果
     */
    @PostMapping("recommendGoods")
    public Result recommendGoodsBase(@Validated({CommonValidate.class})GoodsRecommendOrNotParam param){
        GoodsBaseParam baseParam = GoodsBaseParam.builder()
                .id(param.getId())
                .recommend(param.getRecommend())
                .build();
        return handlerExecutor.execute(() -> baseHandler.updateGoodsBase(baseParam));
    }

}
